<template>
    <div class="content">
        <el-dialog :visible.sync="batchCreateExamination.dialogFormVisible" title="批量创建考场" size="tiny" :before-close="handleClose">
            <div v-loading="upLoading">
               <el-form ref="ruleForm" :model="ruleForm" label-width="87px">
                    <div>
                        <el-row :gutter="41">
                        <el-col :span="24">
                                <el-form-item label="考场总数" prop="classNum" :rules="nameClassNum">
                                    <el-input v-model="ruleForm.classNum" placeholder="请输入考场总数"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </div>
                </el-form>
                <footer>
                    <el-button @click="cancel">取消</el-button>
                    <el-button @click="add('ruleForm',$event)" type="primary">确认</el-button>              
                </footer>
            </div>
        </el-dialog>
    </div>
</template>
<script>
import { getSchoolList, getClass, getGrade } from 'api/classAll';
import { mapGetters } from 'vuex';
export default {
    name: 'addClassSchool',
    props: {
        batchCreateExamination: {
            type: Object,
            required: true
        }
    },
    computed: {
        ...mapGetters(['schoolId', 'schoolName', 'schoolType'])
    },
    data() {
        return {
            upLoading: false,
            gradeList: [],
            ruleForm: {
                provinceId: '',
                cityId: '',
                areaId: '',
                schoolName: '',
                classGrade: '',
                classNum: ''
            },
            schoolTypeDate: '1',
            loading: false,
            schoolList: [],
            rulesSelet: { required: true, message: '请选择', trigger: 'change' },
            rulesSeletcity: {
                validator: (rule, value, callback) => {
                    if (!this.cityId && !value) {
                        callback(new Error('请选择'));
                    } else {
                        callback();
                    }
                },
                trigger: 'change'
            },
            rulesSeletarea: {
                validator: (rule, value, callback) => {
                    if (!this.areaId && !value) {
                        callback(new Error('请选择'));
                    } else {
                        callback();
                    }
                },
                trigger: 'change'
            },
            name: { required: true, message: '请输入学校名称', trigger: 'blur' },
            nameClassNum: [
                { message: '请输入内容', required: true },
                { pattern: /^[0-9]*$/, message: '请输入数字', trigger: 'blur'}
            ],
            allCity: {
                province: [],
                cityList: [],
                CountyList: []
            }
        };
    },
    methods: {
        remoteMethod(query) {
            if (!query) {
                return
            } else {
                this.loading = true;
                setTimeout(() => {
                    this.loading = false;
                    getSchoolList(query, !this.ruleForm.provinceId ? null : this.ruleForm.provinceId, !this.ruleForm.cityId ? null : this.ruleForm.cityId, !this.ruleForm.areaId ? null : this.ruleForm.areaId).then(response => {
                        this.schoolList = response.data.content;
                        // 判断是否培训学校还是普通学校
                        if (this.schoolList[0]) {
                            if (this.schoolList[0].schoolType == '0') {
                                this.schoolTypeDate = '0'
                            } else {
                                this.schoolTypeDate = '1'
                            }
                        } else {
                            this.schoolTypeDate = '1'
                        }
                    });
                }, 200);
            }
        },
    // 判断是否普通学校/培训学校
        judgmentSta(val) {
            this.schoolList.forEach(item => {
                if (val === item.schoolId) {
                    this.$store.dispatch('SetSchoolType', item.schoolType);
                }
            });
        },
    // 年级列表
        queryAllGrade(schoolId) {
            if (schoolId) {
                this.judgmentSta(schoolId);
                this.getGradeName = '';
                this.newSchoolId = schoolId;
                getGrade(!schoolId? null : schoolId).then(response => {
                    this.gradeList = response.data.content;
                });
            }
        },
    // 班级列表
        queryAllClass(gradeId) {
            getClass(!this.newSchoolId ? null : this.newSchoolId, !gradeId ? null : gradeId).then(response => {
                this.classList = response.data.content;
            });
        },
        handleClose() {
            this.batchCreateExamination.dialogFormVisible = false;
        },
        add(ruleForm) {
            this.$refs[ruleForm].validate(valid => {
                if (valid) {
                    if (parseInt(this.ruleForm.classNum) > this.batchCreateExamination.classMaxCount) {
                        if (this.batchCreateExamination.classMaxCount <= 0) {
                            this.$message({
                                message: '最多可批量创建0个！',
                                type: 'warning'
                            });
                            return;
                        }
                        this.$message({
                            message: '最多可批量创建' + this.batchCreateExamination.classMaxCount +'个！',
                            type: 'warning'
                        });
                        return;
                    }
                    this.upLoading = true;
                    this.$emit('addExamState', this.ruleForm.classNum);
                    this.batchCreateExamination.dialogFormVisible = false;
                }
            });
        },
        cancel() {
            this.batchCreateExamination.dialogFormVisible = false;
        }
    }
};
</script>
<style lang="scss" scoped>
.title {
  display: block;
  text-align: left;
  margin-left: 20px;
  font-size: 14px;
  color: #333;
  padding-bottom: 10px;
  position: relative;
  &::after{
    position: absolute;
    width: 4px;
    height: 14px;
    background: #ff5e2c;
    left: -15px;
    top: 9px;
    content: ""
  }
}

footer {
  text-align: center;
}
</style>
